﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition;
using System.ComponentModel;

namespace MefSample
{
    /// <summary>
    /// Logique d'interaction pour App.xaml
    /// </summary>
    public partial class App : Application
    {
        [Import(RequiredCreationPolicy = CreationPolicy.NonShared)]
        public ViewFactory ViewFactory1 { get; set; }

        [Import(RequiredCreationPolicy=CreationPolicy.NonShared)]
        public ViewFactory ViewFactory2 { get; set; }

        //[Import(typeof(ICoco))]
        //public Coco Coco1 { get; set; }

        //[Import(typeof(ICoco))]
        //public Coco Coco2 { get; set; }

        [Import]
        public Toto Coco1 { get; set; }

        [Import]
        public Toto Coco2 { get; set; }

       // [Import]
        //public Titi Titi { get; set; }

        protected override void OnStartup(StartupEventArgs e)
        {
            RegistrationModel2();
            
            base.OnStartup(e);
        }

        private void RegistrationModel2()
        {
            //var registration = new RegistrationBuilder();

            //registration.Implements<IController>().
            //    Export<IController>();

            //var catalog = new AssemblyCatalog(
            //    typeof(Program).Assembly, registration);
            //var container = new CompositionContainer(catalog);

        }

        private void RegistrationModel1()
        {
            var catalog = new AggregateCatalog();
            var assCat = new AssemblyCatalog(typeof(Toto).Assembly);
            catalog.Catalogs.Add(assCat);

            var t = TypeDescriptor.AddAttributes(typeof(Titi), new ExportAttribute(typeof(Titi)));
            catalog.Catalogs.Add(new TypeCatalog(typeof(ViewFactory)));
            catalog.Catalogs.Add(new TypeCatalog(typeof(Toto)));
            catalog.Catalogs.Add(new TypeCatalog(typeof(Titi)));
            var container = new CompositionContainer(catalog);


            //container.ComposeExportedValue<Titi>(new Titi());

            container.ComposeParts(this);
        }
    }

    [Export(typeof(Toto))]
    public class Toto
    {
        
        public Toto()
        {
            id = Guid.NewGuid();
        }
        public Guid id {get; set;}
        public string Prop { get { return "ca c'est mieux"; } }
         [Import(RequiredCreationPolicy = CreationPolicy.NonShared)]
        public ViewFactory ViewFactory { get; set; }
    }

    public class Titi : ITiti
    {

        public Titi()
        {
            id = Guid.NewGuid();
        }
        public Guid id { get; set; }
        public string Prop { get { return "ca c'est mieux"; } }
    }

    interface ITiti
    {
    }

    [Export(typeof(ViewFactory))]
    public class ViewFactory
    {
        public ViewFactory()
        {
            id = Guid.NewGuid();
        }
        public Guid id { get; set; }
        public string Property { get { return "Fais pas chier"; } }
    }   
}
